using System;
using Microsoft.AspNetCore.Mvc;
using QntPlatform.Db;

namespace QntSvc.Web.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LogController:ControllerBase
    {
         [HttpGet]
        public Tuple<long, LogInfo[]> GetLogs(long? ownerId,int? maxLogLv,string txt, int pageNum, int pageSize)
        {
            pageNum = pageNum < 1 ? 1 : pageNum;
            pageSize = pageSize < 1 ? 1 : pageSize;
            var select=LogStore.GetDb().Select<LogInfo>();
            if(ownerId!=null)
            {
               select= select.Where(p=>p.OwnerId==ownerId);
            }
            if(maxLogLv!=null&&maxLogLv>0)
            {
                select=select.Where(p=>p.LogLevel<=maxLogLv);
            }
            if(!string.IsNullOrWhiteSpace(txt))
            {
                //var where=new FreeSql.Internal.Model.DynamicFilterInfo();
                
                select=select.Where(p=>p.Info1.Contains(txt)||p.Info2.Contains(txt)||p.Info3.Contains(txt)||p.Msg.Contains(txt));
            }
            var list =select.Count(out var total).OrderByPropertyName("Id", false).Page(pageNum, pageSize).ToList().ToArray();
            return Tuple.Create(total, list);
        }
    }
}
